package com.knife.bbs.action;


import com.knife.bbs.business.ActiveUser;
import com.knife.bbs.business.BBSRights;
import com.knife.bbs.business.CollectionsService;
import com.knife.bbs.domain.Collections;
import com.knife.bbs.object.BBSDoc;
import com.knife.util.CommUtil;
import com.knife.web.ActionContext;
import com.knife.web.Module;
import com.knife.web.Page;
import com.knife.web.WebForm;
import com.knife.web.tools.IActiveUser;
import com.knife.web.tools.IPageList;

public class CollectionsAction extends BBSAction {
	private CollectionsService service = new CollectionsService();

	public Object doBefore(WebForm form, Module module) {
		if (!BBSRights.checkRights(new Collections(), "list",(ActiveUser)getCurrentUser(form)))
			return new Page("popedomError", "norights.htm", "html","/"+skin+"/");
		return super.doBefore(form, module);
	}

	public IPageList doQuery(WebForm form, int currentPage, int pageSize) {		
		return service.queryUserCollections(getCurrentUser(form).getUserName(),
				pageSize, currentPage);
	}
	
	public Page doList(WebForm form, Module module){
		int currentPage = CommUtil.null2Int(form.get("page"));// 页码
		int pageSize = CommUtil.null2Int(form.get("pageSize"));// 每页大小
		if (currentPage < 1)
			currentPage = 1;
		if (pageSize < 1)
			pageSize = 15;
		IPageList pList = doQuery(form, currentPage, pageSize);
		if (pList != null) {
			CommUtil.saveIPageList2WebForm(pList, form,BBSDoc.class.getName(),com.knife.bbs.domain.BBSDoc.class);
		}
		return module.findPage("/"+skin+"/","list");
	}

	public Object form2Obj(WebForm form) {
		String cid = com.knife.util.CommUtil.null2String(form.get("cid"));
		Collections obj = service.getCollections(cid);
		if (obj == null)obj = new Collections();
		form.toPo(obj);
		//System.out.println("主键盘"+obj.getCid());		
		obj.setUsername(getCurrentUser(form).getUserName());
		return obj;
	}

	public  IActiveUser getCurrentUser(WebForm form) {
		ActiveUser user = (ActiveUser) ActionContext.getContext().getSession()
				.getAttribute("bbsuser");
		return user;
	}

	@Override
	public Page doInit(WebForm arg0, Module arg1) {
		// TODO Auto-generated method stub
		return null;
	}
}
